﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PI_CS_Library.PicsNetwork_Information_Model;
using PI_CS_Library;
using TransportLayer;
using TransportLayer.Manager;
using PI_CS_Tools;
using System.Diagnostics;

namespace PI_CS_GUI
{
    public partial class AddMatrixGUI : Form
    {
        PicsNetworkController _nc;
        GUIDelegate _gui;

        public AddMatrixGUI(PicsNetworkController _nc, GUIDelegate _gui)
        {
            this._nc = _nc;
            this._gui = _gui;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int _newId = Int32.Parse(textBox1.Text);
            string _newName = textBox2.Text;
            string _newType = (string)comboBox1.SelectedItem;
            float _newX = float.Parse(textBox3.Text);
            float _newY = float.Parse(textBox4.Text);
            float _delay;
            if (textBox5.TextLength == 0) _delay = (float)0.1;
            else _delay = float.Parse(textBox5.Text);
            bool _accessible = checkBox1.Checked;
            MatrixParameters _newParameters = new MatrixParameters(_accessible, _delay);
           

            PicsMatrix _newMatrix = _nc.Network.AddNewMatrix(_newId, _newName, _newType, _newX, _newY,_newParameters);

            TransportLayer.Node _transportNode = _newMatrix.GetTransportRepresentation();

            string tempfile = "tmp" + RandomNumber.Get + ".xml";
            _transportNode.save(tempfile);
            Process process = new Process();
            process.StartInfo.FileName = "TransportLayer.ExecNode.exe";
            process.StartInfo.Arguments = tempfile + " " + _nc.ManagerEngine.Port + " " + 1234;
            process.Start();
            while (_nc.ManagerEngine.ConnectedNodes < _nc.Network.MatrixResourceList.Count)
            { }
            _nc.ManagerEngine.HideNode(_newId);
            //_nc.ManagerEngine.Nodes.Add(_newMatrix.Id.Number, _newMatrix.GetEngineRepresentation(process));

            _gui(this, new GUIArgs("newMatrixOK#"+_newName));

            this.Close();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            label7.Visible = true;
            textBox5.Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _gui(this, new GUIArgs("newMatrixCancel"));
            this.Close();
        }

        private void AddMatrixGUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            _gui(this, new GUIArgs("newMatrixCancel"));
        }
    }
}
